WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
solver_eigen.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// wolf includes
23
24// Eigen includes
25#include <Eigen/SparseCore>
26#include <Eigen/SparseQR>
27#include <Eigen/SparseCholesky>
28#include <Eigen/OrderingMethods>
29
30#include <memory>
31
32namespace wolf
33{
35
58{
59 public:
61 {
62 QR,
63 CHOLESKY
64 };
65
66 protected:
67 // problem structure (column and row indexes)
68 std::map<StateBlockPtr, unsigned int> sb_2_col_;
69 std::map<FactorBasePtr, unsigned int> fac_2_row_;
70 std::map<StateBlockPtr, LocalParametrizationBasePtr> sb_2_local_param_;
71 std::set<StateBlockPtr> fixed_state_blocks_;
73 unsigned int n_rows_, n_cols_;
74
75 // linearized problem
76 Eigen::SparseMatrix<double> A_;
77 Eigen::VectorXd b_;
78 double cost_;
79
80 // linear solvers
83 Eigen::SparseQR<Eigen::SparseMatrix<double>, Eigen::NaturalOrdering<int>> qr_;
84 Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> ldlt_;
85 struct SuiteSparseSolvers;
86 std::unique_ptr<SuiteSparseSolvers> suitesparse_;
87 Eigen::SparseMatrix<double> R_, Rt_;
88 Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic, int> R_perm_;
89
90 // params
91 unsigned int max_num_iterations_;
94 unsigned int min_num_iterations_;
95
96 // summary of the last solve
97 unsigned int iterations_;
102 unsigned int line_search_steps_;
106
107 // profiling
110
111 public:
112 SolverEigen(const ProblemPtr& _wolf_problem, const YAML::Node& _params);
113
115
116 ~SolverEigen() override;
117
118 bool computeCovariancesDerived(const CovarianceBlocksToBeComputed _blocks) override;
119 bool computeCovariancesDerived(const std::vector<StateBlockPtr>& st_list) override;
120
121 bool converged() const override;
122 bool failed() const override;
123 bool wasStopped() const override;
124 unsigned int iterations() const override;
125 double initialCost() const override;
126 double finalCost() const override;
127 double totalTime() const override;
128
129 int numStateBlocksEstimatedDerived() const override;
130 int sizeStateBlocksEstimatedDerived() const override;
131 int numFactorsDerived() const override;
132 int sizeFactorsDerived() const override;
133
135
137 std::string getLinearSolverName() const;
138
140 const Eigen::SparseMatrix<double>& getJacobian() const;
141
144 std::string terminationType() const;
145
147 std::string briefReport() const;
148
150 std::string fullReport() const;
151
152 protected:
153 std::string solveDerived(const ReportVerbosity report_level) override;
154
155 void addFactorDerived(const FactorBasePtr& fac_ptr) override;
156 void removeFactorDerived(const FactorBasePtr& fac_ptr) override;
157 void addStateBlockDerived(const StateBlockPtr& state_ptr) override;
158 void removeStateBlockDerived(const StateBlockPtr& state_ptr) override;
159 void updateStateBlockStatusDerived(const StateBlockPtr& state_ptr) override;
160 void updateStateBlockLocalParametrizationDerived(const StateBlockPtr& state_ptr) override;
161
162 bool isFactorRegisteredDerived(const FactorBasePtr& fac_ptr) const override;
163 bool isStateBlockRegisteredDerived(const StateBlockPtr& state_ptr) const override;
164 bool isStateBlockFixedDerived(const StateBlockPtr& st) const override;
165 bool hasThisLocalParametrizationDerived(const StateBlockPtr& st,
166 const LocalParametrizationBasePtr& local_param) const override;
167 bool hasLocalParametrizationDerived(const StateBlockPtr& st) const override;
168
169 void printProfilingDerived(std::ostream& _stream) const override;
170 bool checkDerived(std::string prefix = "") const override;
171
173 void reindex();
174
177 bool linearize(bool _apply_loss);
178
180 bool factorize();
181
183 Eigen::VectorXd solveIncrement() const;
184
186 Eigen::MatrixXd solveNormalEquations(const Eigen::MatrixXd& _E) const;
187
189 bool computeCovarianceBlocks(const std::vector<std::pair<StateBlockPtr, StateBlockPtr>>& _pairs);
190
192 void applyIncrement(const Eigen::VectorXd& _dx);
193};
194
196{
197 return linear_solver_;
198}
199
200inline const Eigen::SparseMatrix<double>& SolverEigen::getJacobian() const
201{
202 return A_;
203}
204
205} // namespace wolf
Gauss-Newton solver using sparse linear solvers through Eigen (no ceres dependency)
Definition: solver_eigen.h:58
unsigned int n_rows_
Definition: solver_eigen.h:73
SolverEigen(const ProblemPtr &_wolf_problem, const YAML::Node &_params)
Definition: solver_eigen.cpp:92
void reindex()
Recomputes the column of each non-fixed state block and the row of each factor (if structure changed)
Definition: solver_eigen.cpp:365
unsigned int max_num_iterations_
Definition: solver_eigen.h:91
bool hasLocalParametrizationDerived(const StateBlockPtr &st) const override
Definition: solver_eigen.cpp:1025
bool converged_
Definition: solver_eigen.h:99
unsigned int line_search_steps_
step halvings of the backtracking line search
Definition: solver_eigen.h:102
std::map< FactorBasePtr, unsigned int > fac_2_row_
first row of each factor
Definition: solver_eigen.h:69
double linear_solver_time_
Definition: solver_eigen.h:105
std::string getLinearSolverName() const
Linear solver and its implementation, e.g. "cholesky (CHOLMOD)" or "qr (Eigen SparseQR)".
Definition: solver_eigen.cpp:1264
std::string termination_message_
Definition: solver_eigen.h:100
std::set< StateBlockPtr > fixed_state_blocks_
registered fixed state blocks
Definition: solver_eigen.h:71
unsigned int n_failed_
Definition: solver_eigen.h:109
double parameter_tolerance_
Definition: solver_eigen.h:92
bool linearize(bool _apply_loss)
Definition: solver_eigen.cpp:445
Eigen::SparseMatrix< double > A_
stacked jacobian (weighted by the loss function if applicable)
Definition: solver_eigen.h:76
WOLF_SOLVER_CREATE(SolverEigen)
void printProfilingDerived(std::ostream &_stream) const override
Definition: solver_eigen.cpp:1092
unsigned int n_no_convergence_
Definition: solver_eigen.h:109
unsigned int n_cols_
Definition: solver_eigen.h:73
unsigned int successful_steps_
Definition: solver_eigen.h:101
Eigen::PermutationMatrix< Eigen::Dynamic, Eigen::Dynamic, int > R_perm_
column permutation of the QR
Definition: solver_eigen.h:88
const Eigen::SparseMatrix< double > & getJacobian() const
Stacked jacobian of the last linearization (with loss function weights)
Definition: solver_eigen.h:200
double initialCost() const override
Definition: solver_eigen.cpp:1077
unsigned int min_num_iterations_
iterations during which the solver cannot be interrupted
Definition: solver_eigen.h:94
bool stopped_
Definition: solver_eigen.h:99
Eigen::VectorXd b_
stacked residuals (weighted by the loss function if applicable)
Definition: solver_eigen.h:77
void addStateBlockDerived(const StateBlockPtr &state_ptr) override
Definition: solver_eigen.cpp:961
void applyIncrement(const Eigen::VectorXd &_dx)
Applies an increment (in the tangent space) to the non-fixed state blocks.
Definition: solver_eigen.cpp:585
bool isStateBlockFixedDerived(const StateBlockPtr &st) const override
Definition: solver_eigen.cpp:1014
bool isStateBlockRegisteredDerived(const StateBlockPtr &state_ptr) const override
Definition: solver_eigen.cpp:1009
unsigned int n_linear_solves_
Definition: solver_eigen.h:103
LinearSolverType getLinearSolverType() const
Definition: solver_eigen.h:195
std::string briefReport() const
One line report of the last solve (as ceres::Solver::Summary::BriefReport())
Definition: solver_eigen.cpp:1182
unsigned int n_convergence_
Definition: solver_eigen.h:109
LinearSolverType linear_solver_
Definition: solver_eigen.h:81
double cost_
Definition: solver_eigen.h:78
int sizeStateBlocksEstimatedDerived() const override
Definition: solver_eigen.cpp:1040
Eigen::SparseMatrix< double > Rt_
(sorted) R factor of the QR and its transpose, used for covariances
Definition: solver_eigen.h:87
bool computeCovarianceBlocks(const std::vector< std::pair< StateBlockPtr, StateBlockPtr > > &_pairs)
Computes and stores in the problem the covariance blocks of the given pairs of state blocks.
Definition: solver_eigen.cpp:863
unsigned int n_linearizations_
Definition: solver_eigen.h:103
std::string terminationType() const
Definition: solver_eigen.cpp:1174
unsigned int iterations() const override
Definition: solver_eigen.cpp:1072
bool failed() const override
Definition: solver_eigen.cpp:1062
void updateStateBlockStatusDerived(const StateBlockPtr &state_ptr) override
Definition: solver_eigen.cpp:982
Eigen::SparseQR< Eigen::SparseMatrix< double >, Eigen::NaturalOrdering< int > > qr_
columns ordered in reindex()
Definition: solver_eigen.h:83
std::unique_ptr< SuiteSparseSolvers > suitesparse_
SPQR and CHOLMOD (defined only if SuiteSparse was found)
Definition: solver_eigen.h:86
double function_tolerance_
Definition: solver_eigen.h:92
double minimizer_time_
Definition: solver_eigen.h:104
std::map< StateBlockPtr, LocalParametrizationBasePtr > sb_2_local_param_
local param of each state block
Definition: solver_eigen.h:70
unsigned int n_iter_max_
Definition: solver_eigen.h:108
LinearSolverType
Definition: solver_eigen.h:61
double gradient_tolerance_
Definition: solver_eigen.h:92
int numStateBlocksEstimatedDerived() const override
Definition: solver_eigen.cpp:1030
void updateStateBlockLocalParametrizationDerived(const StateBlockPtr &state_ptr) override
Definition: solver_eigen.cpp:994
bool hasThisLocalParametrizationDerived(const StateBlockPtr &st, const LocalParametrizationBasePtr &local_param) const override
Definition: solver_eigen.cpp:1019
bool failed_
Definition: solver_eigen.h:99
Eigen::MatrixXd solveNormalEquations(const Eigen::MatrixXd &_E) const
Solves (AᵀA)·X = E using the last factorization.
Definition: solver_eigen.cpp:569
bool converged() const override
Definition: solver_eigen.cpp:1057
double total_time_
Definition: solver_eigen.h:98
void removeFactorDerived(const FactorBasePtr &fac_ptr) override
Definition: solver_eigen.cpp:953
std::string solveDerived(const ReportVerbosity report_level) override
Definition: solver_eigen.cpp:154
Eigen::SimplicialLDLT< Eigen::SparseMatrix< double > > ldlt_
Definition: solver_eigen.h:84
std::string fullReport() const
Full report of the last solve (as ceres::Solver::Summary::FullReport())
Definition: solver_eigen.cpp:1191
bool isFactorRegisteredDerived(const FactorBasePtr &fac_ptr) const override
Definition: solver_eigen.cpp:1004
void addFactorDerived(const FactorBasePtr &fac_ptr) override
Definition: solver_eigen.cpp:943
bool computeCovariancesDerived(const CovarianceBlocksToBeComputed _blocks) override
Definition: solver_eigen.cpp:605
bool wasStopped() const override
Definition: solver_eigen.cpp:1067
bool checkDerived(std::string prefix="") const override
Definition: solver_eigen.cpp:1105
double postprocessor_time_
Definition: solver_eigen.h:104
double linearization_time_
Definition: solver_eigen.h:105
double finalCost() const override
Definition: solver_eigen.cpp:1082
bool interrupt_on_problem_change_
interrupt the solver if the problem changed (as SolverCeres)
Definition: solver_eigen.h:93
double totalTime() const override
Definition: solver_eigen.cpp:1087
double initial_cost_
Definition: solver_eigen.h:98
unsigned int unsuccessful_steps_
accepted and rejected Gauss-Newton steps
Definition: solver_eigen.h:101
int sizeFactorsDerived() const override
Definition: solver_eigen.cpp:1048
unsigned int iterations_
Definition: solver_eigen.h:97
double preprocessor_time_
Definition: solver_eigen.h:104
Eigen::VectorXd solveIncrement() const
Solves A_·dx = -b_ using the last factorization.
Definition: solver_eigen.cpp:552
std::map< StateBlockPtr, unsigned int > sb_2_col_
first column of each non-fixed state block
Definition: solver_eigen.h:68
bool factorize()
Factorizes A_ with the selected linear solver. Returns false if it failed or A_ is rank deficient.
Definition: solver_eigen.cpp:518
double final_cost_
Definition: solver_eigen.h:98
unsigned int n_iter_acc_
Definition: solver_eigen.h:108
bool analysis_dirty_
the structure changed: symbolic analysis of the factorization needed
Definition: solver_eigen.h:82
Eigen::SparseMatrix< double > R_
Definition: solver_eigen.h:87
int numFactorsDerived() const override
Definition: solver_eigen.cpp:1035
bool structure_dirty_
Definition: solver_eigen.h:72
void removeStateBlockDerived(const StateBlockPtr &state_ptr) override
Definition: solver_eigen.cpp:971
~SolverEigen() override
Definition: solver_eigen.cpp:146
unsigned int n_interrupted_
Definition: solver_eigen.h:109
Solver manager for WOLF.
Definition: solver_manager.h:84
CovarianceBlocksToBeComputed
Enumeration of covariance blocks to be computed.
Definition: solver_manager.h:89
ReportVerbosity
Enumeration for the verbosity of the solver report.
Definition: solver_manager.h:102
Definition: capture_base.h:23
WOLF_PTR_TYPEDEFS(CaptureLandmarksExternal)
Definition: solver_eigen.cpp:60